import Vue from 'vue'
import Vuex from 'vuex'
import axios from 'axios'

Vue.use(Vuex)

let store =  new Vuex.Store({
    state: {
        navlist:[],  //导航菜单的数据
        list:[]      //列表
    },
    //同步的操作
    mutations: {
        //导航数据
        setNavList(state,navlist){
            state.navlist = navlist;
        },
        setList(state,list){
            state.list = list;
        },
        changeNav(state,id){
            state.navlist.forEach(item => {
                if(item.id === id){
                    item.isAdd = !item.isAdd;
                }
            })
            state.navlist = [...state.navlist];
        }
    },
    actions: {
        //禁止在action里直接修改state，在actions通过commit提交mutations里的方法
        getNavList({commit}){
            //异步
            axios.get('/api/navList').then(res => {
                commit('setNavList',res.data.data)
            })
        },
        //获取列表数据
        getList({commit}){
            axios.get('/api/list').then(res => {
                commit('setList',res.data.data)
            })
        }
    },
    modules: {

    }
})

window.store = store;

export default store